/*
 * University Of Washington - Tacoma
 * TCSS-422, Operating Systems
 * Assignment 1 - Web Crawler
 * April 26, 2011
 * 
 * Team: Kernel Monkeys
 * Andrew Boguk
 * Sergiy Kulyk
 * Nicholas Swartzendruber
 */

package crawler.engine;

import java.util.Map;

import crawler.engine.DataGatherer.WordPair;


/**
 * The reporter object is used to take information 
 * from the data gatherer, collect it, and deliver
 * it to a UI. A reporter should be passed to the UI
 * every time a page has finished parsing. 
 * 
 * @author Nicholas Swartzendruber
 * @version 1.0
 */
public class Reporter {

	//Fields
	private final String data;
	
	/**
	 * Constructs the reporter object based on the
	 * data that has been passed. The object is
	 * immutable, so once the data has been passed,
	 * it can not change. 
	 * 
	 * @param webpage The webpage last visited.
	 * @param webpageNum The number of webpages parsed so far.
	 * @param wordCount The total number of words hit.
	 * @param urlCount The total number of URL's hit.
	 * @param words A map of words to avg and total hits.
	 * @param pageTime Avg time in ms that the pages take to parse.
	 * @param totalTime The total time the engine has been parsing.
	 */
	public Reporter(final String webpage,
					final int webpageNum,
					final int wordCount,
					final int urlCount,
					final Map<String, WordPair> words,
					final int pageTime,
					final int totalTime) {
		StringBuilder sb = new StringBuilder();
		sb.append("Parsed: ").append(webpage).append("\n");
		sb.append("Pages Retrieved: ").append(webpageNum).append("\n");
		sb.append("Average words per page: ").append(wordCount).append("\n");
		sb.append("Average URLs per page: ").append(urlCount).append("\n");
		
		sb.append(String.format("  %-20s%-25s%-20s\n", "Keyword", "Ave. hits per page", "Total hits"));
		
		String format = "  %-20s%-25.4f%-20d\n";
		
		for(String word : words.keySet()) {
			WordPair wp = words.get(word);
			sb.append(String.format(format, word, wp.avg, wp.total));
		}
		sb.append("Page limit: ").append(Engine.MAX_PAGES).append("\n");
		sb.append("Average parse time per page: ").append(pageTime).append(" msec\n");
		sb.append("Total running time: ").append(String.format("%.4f sec", totalTime / 1000.0)).append("\n");
		data = sb.toString();
	}
	
	/**
	 * When called, the reporter will construct a pre-formatted
	 * string that contains all of the data that this reporter object
	 * is reporting about.
	 *  
	 * @return A pre-formatted string containing statistical
	 * 			data about parsed pages up to a certain point. 
	 */
	public String getFormattedData (){
		return data;
	}
}
